package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.mapper.AcademyMapper;
import com.ddwl.goods.dao.model.Academy;
import com.ddwl.schema.bo.goods.AcademyCheckPageBo;
import com.ddwl.schema.bo.goods.AcademySearchBo;
import com.ddwl.schema.bo.goods.QueryAcademyBo;
import com.ddwl.schema.bo.goods.SelfAcademyQueryBo;
import com.ddwl.schema.vo.goods.AcademyVo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author wenbin
 */
@Repository
public class AcademyDao extends TkDao<AcademyMapper, Academy> {

    private String sort ="academySort";

    @Resource
    private AcademyMapper academyMapper;

    public List<Academy> queryAcademy(QueryAcademyBo bo) {
        Example example = getExample();
        example.and().andEqualTo("id",bo.getId())
                .andEqualTo("status", GoodsEnum.AcademyStatusEnum.PASSED.getCode())
                .andLike("title", MySqlUtil.allLike(bo.getTitle()))
                .andEqualTo("effect",bo.getEffect())
                .andEqualTo("source",bo.getSource())
                .andEqualTo("academyCategoryId",bo.getAcademyCategoryId())
                .andGreaterThanOrEqualTo("gmtCreated",bo.getStartTime())
                .andLessThanOrEqualTo("gmtCreated",bo.getEndTime());
        //app查询按照序号排序取前三条
        if (bo.getAcademy()!=null&&sort.equals(bo.getAcademy())) {
            example.setOrderByClause("academy_sort desc,gmt_created limit 3");
        }
        return baseMapper.selectByExample(example);
    }

    public void updateSelfAcademy(Academy entity) {
        academyMapper.updateByPrimaryKey(entity);
    }

    public List<Academy> getMyAcademyList(SelfAcademyQueryBo bo) {
        Example example = getExample();
        example.and().andEqualTo("createdUserId", bo.getCreatedUserId());
        if(GoodsEnum.AcademyStatusEnum.DRAFT.getCode().equals(bo.getStatus())){
            example.and().andIn("status", Arrays.asList(0,2));
        }else{
            example.and().andEqualTo("status", bo.getStatus());
        }
        return baseMapper.selectByExample(example);
    }

    public List<AcademyVo> getNeedCheckAcademyList(AcademyCheckPageBo param) {
        return academyMapper.getNeedCheckAcademyList(param);
    }

    public List<AcademyVo> searchAcademy(AcademySearchBo param) {
        return academyMapper.searchAcademy(param);
    }

    public void updateScanCount(Academy academy) {
        academyMapper.updateByPrimaryKeySelective(academy);
    }

    public Integer getAcademyUnApproveList(QueryAcademyBo param) {
        return academyMapper.getAcademyUnApproveList(param);
    }

    public List<Academy> academyListForApp(QueryAcademyBo bo) {
        Example example = getExample();
        example.and().andEqualTo("academyCategoryId",bo.getAcademyCategoryId())
                .andEqualTo("status", GoodsEnum.AcademyStatusEnum.PASSED.getCode())
                .andEqualTo("effect", BaseEnum.StatusEnum.ENABLE.getCode());
        example.setOrderByClause("academy_sort desc,gmt_modified desc");
        return baseMapper.selectByExample(example);
    }
}
